package com.koron.common.core.business.message.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.business.message.bean.dto.AccountPhone;
import com.koron.common.core.business.message.bean.dto.MessageTopicDto;
import com.koron.common.core.business.message.bean.po.MessageTemplatePo;
import com.koron.common.core.business.message.bean.po.MessageTopicPo;
import com.koron.common.core.business.message.bean.vo.MessageTopicVo;
import com.koron.common.core.business.message.mapper.MessageTopicMapper;
import com.koron.common.core.business.message.service.MessageTemplateService;
import com.koron.common.core.business.message.service.MessageTopicService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * 消息主题表(MessageTopic)表服务实现类
 *
 * @author zhouj
 * @since 2022-03-16 14:14:09
 */
@Service("messageTopicService")
public class MessageTopicServiceImpl implements MessageTopicService {

    @Autowired
    MessageTemplateService templateService;

    @Override
    @TaskAnnotation("insertOrUpdate")
    public MessageTopicPo insertOrUpdate(SessionFactory factory, MessageTopicDto messageTopicDto) {

        MessageTopicMapper mapper = factory.getMapper(MessageTopicMapper.class);

        MessageTopicPo messageTopicPo = new MessageTopicPo();

        BeanUtils.copyProperties(messageTopicDto, messageTopicPo);

        Assert.isTrue(StringUtils.isNotEmpty(messageTopicPo.getTopicCode()), "主题编码不能为空");

        //新建时,判断主题编码不能存在
        if (StringUtils.isEmpty(messageTopicDto.getId())) {

            MessageTopicDto topicDto = queryByCode(factory, messageTopicPo.getTopicCode());

            Assert.isNull(topicDto, "已存在该主题编码");

        }

        mapper.insertOrUpdate(messageTopicPo);

        List<MessageTemplatePo> templatePoList = messageTopicDto.getTemplatePoList();

        Assert.isTrue(CollectionUtils.isNotEmpty(templatePoList));

        templatePoList.forEach(p -> p.setTopicId(messageTopicPo.getId()));

        templateService.insertBatch(factory, templatePoList);

        return messageTopicPo;
    }

    @Override
    @TaskAnnotation("insertOrUpdateBatch")
    public int insertOrUpdateBatch(SessionFactory factory, List<MessageTopicPo> messageTopicList) {

        MessageTopicMapper mapper = factory.getMapper(MessageTopicMapper.class);

        return mapper.insertOrUpdateBatch(messageTopicList);
    }

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, MessageTopicPo messageTopic) {

        MessageTopicMapper mapper = factory.getMapper(MessageTopicMapper.class);

        return mapper.insert(messageTopic);
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<MessageTopicPo> messageTopicList) {

        MessageTopicMapper mapper = factory.getMapper(MessageTopicMapper.class);

        return mapper.insertBatch(messageTopicList);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, MessageTopicPo messageTopic) {
        MessageTopicMapper mapper = factory.getMapper(MessageTopicMapper.class);
        return mapper.update(messageTopic);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {

        MessageTopicMapper mapper = factory.getMapper(MessageTopicMapper.class);
        return mapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("queryById")
    public MessageTopicVo queryById(SessionFactory factory, String id) {

        MessageTopicMapper mapper = factory.getMapper(MessageTopicMapper.class);


        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<MessageTopicPo> queryList(SessionFactory factory, MessageTopicPo messageTopic) {

        MessageTopicMapper mapper = factory.getMapper(MessageTopicMapper.class);

        List<MessageTopicPo> list = mapper.queryList(messageTopic);

        return list;
    }

    @Override
    @TaskAnnotation("queryByCode")
    public MessageTopicDto queryByCode(SessionFactory factory, String code) {
        MessageTopicMapper mapper = factory.getMapper(MessageTopicMapper.class);

        MessageTopicDto topicDto = mapper.queryByCode(code);

        return topicDto;
    }

    @Override
    @TaskAnnotation("queryPageList")
    public PageInfo queryPageList(SessionFactory factory, MessageTopicPo messageTopic, PageQuery pageQuery) {

        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy(false));
        MessageTopicMapper mapper = factory.getMapper(MessageTopicMapper.class);

        List<MessageTopicPo> list = mapper.queryList(messageTopic);

        return new PageInfo(list);
    }

    @Override
    @TaskAnnotation("queryPhoneByAccountList")
    public List<AccountPhone> queryPhoneByAccountList(SessionFactory factory, List<String> accountList) {
        MessageTopicMapper mapper = factory.getMapper(MessageTopicMapper.class);

        List<AccountPhone> list = mapper.queryPhoneByAccountList(accountList);

        return list;
    }

}